﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     audioplaythread.cpp
// 描述:
// 作者:      颜磊
// 创建工程：   AudioComment
// 创建时间:   2024-06-26 16:55
///////////////////////////////////////////////////////////////////////////
#include "audioplaythread.h"

#include <QFile>
#include <QDebug>

AudioPlayThread::AudioPlayThread(QObject *parent) : QThread(parent)
{
    setParamed = false;
    loopPlay = false;
}

AudioPlayThread::~AudioPlayThread()
{

}

void AudioPlayThread::stop()
{
    isRun = false;
}

void AudioPlayThread::setPlayParam(uint16_t m_headerLen,uint64_t m_fileDataSize,uint32_t m_sampleRate, uint16_t m_channleNumber, uint16_t m_bitsPerSample)
{
    setParamed = true;
    headerLen = m_headerLen;
    fileDataSiz = m_fileDataSize;
    sampleRate = m_sampleRate;
    channleNumber = m_channleNumber;
    bitsPerSample = m_bitsPerSample;
}

void AudioPlayThread::setAudioFilePath(const QString &value)
{
    audioFilePath = value;
}

void AudioPlayThread::setPlayBeginEnd(uint64_t m_playStartIndex, uint64_t m_playStopIndex)
{
    playStartIndex = m_playStartIndex;
    playStopIndex = m_playStopIndex;

    playNowIndex = playStartIndex/(bitsPerSample/8)*(bitsPerSample/8) + headerLen;
}

void AudioPlayThread::setPlayNowIndex(uint64_t nowIndex)
{
    playNowIndex = nowIndex/(bitsPerSample/8)*(bitsPerSample/8) + headerLen;
}

void AudioPlayThread::setLoopPlay(bool value)
{
    loopPlay = value;
}

void AudioPlayThread::run()
{
    isRun = true;
    if(!setParamed){
        //告诉界面请设置参数
        qDebug() << "未写入配置";
        return;
    }
    QAudioFormat audioFormat;//设置采样格式
    audioFormat.setSampleRate(sampleRate);//设置采样率
    audioFormat.setChannelCount(channleNumber);//设置通道数
    audioFormat.setSampleSize(bitsPerSample);
    audioFormat.setCodec("audio/pcm");//设置编码方式
    audioFormat.setByteOrder(QAudioFormat::LittleEndian);//设置字节序
    audioFormat.setSampleType(QAudioFormat::SignedInt);//设置样本数据类型
    audioOut = new QAudioOutput(audioFormat);
    audioOut->setBufferSize(1 * sampleRate * (bitsPerSample/8) * channleNumber);
    audio = audioOut->start();

    emit signal_playState(true);
    QFile audioFile(audioFilePath);
    if(audioFile.open(QFile::ReadOnly)){

        audioFile.seek(playNowIndex);

        int readSize = sampleRate * (bitsPerSample/8) * channleNumber;
        QByteArray playData;
        while (isRun) {
           // qDebug() << "读取数据";
            audioFile.seek(playNowIndex);
            int readLen = readSize;
            if(playNowIndex + readLen > playStopIndex + headerLen){
                readLen = playStopIndex + headerLen - playNowIndex;
            }
            playData = audioFile.read(readLen);
            while(isRun && playData.size() > 0){
                if(audioOut->bytesFree() <= 2048){ //等待数据播放完再写入
                    continue;
                }
                //qDebug() << "写入数据";
                int writeLen = 2048;
                if(playData.size() < writeLen)
                    writeLen = playData.size();
                audio->write(playData.data(),writeLen);
                playData.remove(0,writeLen);
                playNowIndex += writeLen;
                signal_playIndex(playNowIndex - headerLen);
            }
            if(playNowIndex == playStopIndex+headerLen){
                playNowIndex = playStartIndex/(bitsPerSample/8)*(bitsPerSample/8) + headerLen; //从文件起点或者段起点
                audioOut->stop();
                audio = audioOut->start();
                if(!loopPlay){
                    break;
                }
            }
        }
        audioFile.close();
    }{
        //文件打开失败处理

    }
    audioOut->stop();
    delete audioOut;
    emit signal_playState(false);
}
